#!/bin/bash

if [[ "$SOURCE_BRANCH" = "develop" ]]; then
    # add -d flag to checkout.bash and update image dependencies
    sed -i "s|checkout.bash|checkout.bash -d|g; s|usdotfhwastol/|usdotfhwastoldev/|g; s|:[0-9]*\.[0-9]*\.[0-9]*|:develop|g; s|:CARMA[a-zA-Z]*_[0-9]*\.[0-9]*\.[0-9]*|:develop|g; s|:carma-[a-zA-Z]*-[0-9]*\.[0-9]*\.[0-9]*|:develop|g" \
        Dockerfile
elif [[ "$SOURCE_BRANCH" =~ ^release/.*$ ]]; then

    # Update Dockerfile to point to release images
    RELEASE_NAME=$(echo $SOURCE_BRANCH | cut -d "/" -f 2)
    sed -i "s|usdotfhwastol/|usdotfhwastolcandidate/|g; s|usdotfhwastoldev/|usdotfhwastolcandidate/|g; s|:[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:CARMA[a-zA-Z]*_[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:carma-[a-zA-Z]*-[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:develop|:$RELEASE_NAME|g" \
        Dockerfile
    # swap checkout branch in checkout.bash to release branch
    sed -i "/usdot-fhwa-stol/ s|--branch .*|--branch $SOURCE_BRANCH|g" \

    # swap checkout branch in checkout.bash to release branch
    RELEASE_NAME=$(echo $SOURCE_BRANCH | cut -d "/" -f 2)
    sed -i "s|usdotfhwastol/|usdotfhwastolcandidate/|g; s|usdotfhwastoldev/|usdotfhwastolcandidate/|g; s|:[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:CARMA[a-zA-Z]*_[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:carma-[a-zA-Z]*-[0-9]*\.[0-9]*\.[0-9]*|:$RELEASE_NAME|g; s|:develop|:$RELEASE_NAME|g" \
        Dockerfile
    sed -i "s|--branch develop|--branch $SOURCE_BRANCH|g" \

        docker/checkout.bash
fi
